package com.testmanager.testmananger.shuiyou.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class OtherRedis {

    public static final String JUNDUANREDIS = "JUNDUANREDIS";


    @Value("${spring.redis-other.host}")
    String hostName;

    @Value("${spring.redis-other.port}") int port;
    @Value("${spring.redis-other.database}") int database;
    @Value("${spring.redis-other.jedis.pool.max-active}") int maxActive;
    @Value("${spring.redis-other.jedis.pool.max-idle}") int maxIdle;
    @Value("${spring.redis-other.jedis.pool.min-idle}") int minIdle;
    @Value("${spring.redis-other.timeout}") long maxWait;
    @Value("${spring.redis-other.auth}")String auth;


    public RedisTemplate<String,Object> getJuduanRedisTemplate( ) {



       // System.out.println(hostName+port+password);
        log.info("初始化redisother");
        RedisConnectionFactory factory = this.getRedisConnectionFactory(
                hostName, port, maxActive, maxIdle, minIdle, maxWait,
                database,auth); // 建立Redis的连接
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化类型
        //redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // value的序列化类型
        redisTemplate.setValueSerializer(new GenericToStringSerializer(String.class));
        return redisTemplate;

    }






    @Bean("redisContainer")
    public Map<String,RedisTemplate<String,Object>>  getRedisTemplateContain(){

        return  new HashMap<String,RedisTemplate<String, Object>>();
    }

    public RedisTemplate<String,Object>  getRedisTemplate(){
        if (getRedisTemplateContain().containsKey(JUNDUANREDIS)){
            return getRedisTemplateContain().get(JUNDUANREDIS);
        }else {
            RedisTemplate<String,Object> jundunRedis = getJuduanRedisTemplate();
            getRedisTemplateContain().put(JUNDUANREDIS,jundunRedis);
            return jundunRedis;
        }

    }










    public RedisConnectionFactory getRedisConnectionFactory(String hostName,
                                                             int port, int maxActive, int maxIdle, int minIdle,
                                                            long maxWait, int database,String password) { // 是负责建立Factory的连接工厂类
        JedisConnectionFactory jedisFactory = new JedisConnectionFactory();

        jedisFactory.setHostName(hostName);
        jedisFactory.setPort(port);
        jedisFactory.setPassword(password);
        jedisFactory.setDatabase(database);
        JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxWaitMillis(maxWait);
        jedisFactory.setPoolConfig(poolConfig);
        jedisFactory.afterPropertiesSet(); // 初始化连接池配置
        return jedisFactory;
    }
}
